package com.app.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.app.mapper.PatrolGroupMapper;
import com.app.model.PatrolGroup;
import com.app.service.IPatrolGroupService;
import com.app.util.FindChildrenUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 巡察组 服务实现类
 * </p>
 *
 * @author ghoul
 * @since 2024-12-17
 */
@Service
public class PatrolGroupServiceImpl extends ServiceImpl<PatrolGroupMapper, PatrolGroup> implements IPatrolGroupService {

    @Override
    public List<PatrolGroup> getAdminList(Integer type) {
        QueryWrapper<PatrolGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid", 0);
        queryWrapper.eq("is_del", false);
        if (type == null) {
            queryWrapper.eq("status", 1);
        }
        List<PatrolGroup> list = list(queryWrapper);
        for (PatrolGroup patrolGroup : list) {
            QueryWrapper<PatrolGroup> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("pid", patrolGroup.getId());
            queryWrapper1.eq("is_del", false);
            List<PatrolGroup> groupList = list(queryWrapper1);
            StringBuilder sb = new StringBuilder();
            for (PatrolGroup group : groupList) {
                if (sb.length() > 0) {
                    sb.append(",");
                }
                sb.append(group.getName());
            }
            patrolGroup.setInspection(sb.toString());
        }
        return list;
    }

    @Override
    public boolean create(PatrolGroup request) {
        if (ObjectUtil.isNotNull(request.getId())) {
            request.setUpdateTime(LocalDateTime.now());
            return updateById(request);
        }
        request.setCreateTime(LocalDateTime.now());
        return save(request);
    }

    @Override
    public boolean deleteById(Integer id) {
        PatrolGroup patrolGroup = getById(id);
        if (ObjectUtil.isNotNull(patrolGroup)) {
            patrolGroup.setIsDel(true);
            return updateById(patrolGroup);
        }
        return false;
    }

    @Override
    public PatrolGroup getDetail(Integer id) {
        return getById(id);
    }

    @Override
    public List<PatrolGroup> getCategoryTree(List<PatrolGroup> patrolGroupList) {
        return FindChildrenUtil.findGroupChildren(patrolGroupList, 0);
    }

    @Override
    public boolean updateStatus(Integer id, Integer status) {
        // 获取当前要更新的巡察组信息
        PatrolGroup currentGroup = getById(id);
        if (ObjectUtil.isNull(currentGroup)) {
            return false;
        }

        // 如果是子级（pid != 0）
        if (currentGroup.getPid() != 0) {
            // status为1(显示)时，检查父级状态
            if (status == 1) {
                PatrolGroup parentGroup = getById(currentGroup.getPid());
                // 如果父级是关闭状态，则需要同时开启父级
                if (parentGroup != null && parentGroup.getStatus() == 0) {
                    // 开启父级
                    parentGroup.setStatus(1);
                    updateById(parentGroup);
                }
            }
            // 更新当前子级状态
            currentGroup.setStatus(status);
            return updateById(currentGroup);
        }
        // 如果是父级（pid == 0）
        else {
            // status为0(关闭)时，需要关闭所有子级
            if (status == 0) {
                QueryWrapper<PatrolGroup> childWrapper = new QueryWrapper<>();
                childWrapper.eq("pid", id);
                childWrapper.eq("is_del", false);
                List<PatrolGroup> childList = list(childWrapper);
                for (PatrolGroup child : childList) {
                    child.setStatus(0);
                    updateById(child);
                }
            }
            // 更新当前父级状态
            currentGroup.setStatus(status);
            return updateById(currentGroup);
        }
    }

}
